from django.db import models

from company.models import Vehicle


class Path(models.Model):
    """路线
    - 设置路线: 0x8606
    """
    # 808
    attribute = models.IntegerField(
        default=0b0000_0000,
        verbose_name="""
        路线属性，0x00-0xFF
        位      标志
        --      ----
        0       1：根据时间
        1       保留
        2       1：进路线报警给驾驶员
        3       1：进路线报警给平台
        4       1：出路线报警给驾驶员
        5       1：出路线报警给平台
        6-15    保留
        """,
    )
    start_time = models.DateTimeField(blank=True, null=True, verbose_name='起始时间')
    end_time = models.DateTimeField(blank=True, null=True, verbose_name='结束时间')
    points_num = models.IntegerField(verbose_name='总拐点数，0x00-0xFF')
    # 自定义
    created_time = models.DateTimeField(auto_now_add=True, editable=False)
    modified_on = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'path'


class PathPoint(models.Model):
    """路线拐点项
    """
    # 808
    point_id = models.IntegerField(verbose_name='当前路线中的第N个拐点，0x0000-0xFFFF')
    path = models.ForeignKey(Path, on_delete=models.CASCADE, db_index=True)
    longitude = models.DecimalField(max_digits=30, decimal_places=25, verbose_name='经度')
    latitude = models.DecimalField(max_digits=30, decimal_places=25, verbose_name='纬度')
    weight = models.FloatField(default=5, verbose_name='路段宽度，单位为米，0x00-0xFF')
    attribute = models.SmallIntegerField(
        default=0b0000_0100,
        verbose_name="""
        路段属性，0x00-0xFF
        位      标志
        --      ----
        0       1：行驶时间
        1       1：限速
        2       0：北纬；1：南纬
        3       0：东经；1：西经
        4-7     保留
        """,
    )
    driving_threshold = models.IntegerField(blank=True, null=True, verbose_name='路段行驶时间超出阈值，单位为秒，若路段属性0位为0则没有该字段')
    under_driving_threshold = models.IntegerField(blank=True, null=True, verbose_name='路段行驶时间不足阈值，单位为秒，若路段属性0位为0则没有该字段')
    max_speed = models.IntegerField(blank=True, null=True, verbose_name='路段最高限速，单位为km/h，若路段属性1位为0则没有该字段')
    over_speed_duration = models.IntegerField(blank=True, null=True, verbose_name='超速持续时间，单位为秒，若路段属性1位为0则没有该字段')
    # 自定义
    created_time = models.DateTimeField(auto_now_add=True, editable=False)
    modified_on = models.DateTimeField(auto_now=True)

    @property
    def section_id(self):
        """路段ID 808
        0x0000-0xFFFF
        路段为该拐点到下一拐点
        """
        return self.point_id

    class Meta:
        db_table = 'path_point'


class PathVehicle(models.Model):
    """线路车辆关系表
    """
    StatusChoice = (
        ('ACTIVE', '启用'),
        ('DEACTIVE', '停用'),
    )

    path = models.ForeignKey(Path, on_delete=models.CASCADE, db_index=True)
    vehicle = models.ForeignKey(Vehicle, on_delete=models.CASCADE, db_index=True)
    status = models.CharField(max_length=10, choices=StatusChoice, default=StatusChoice[0][0])
    created_time = models.DateTimeField(auto_now_add=True, editable=False)
    modified_on = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'relation_path_vehicle'
